package com.example.demo.controller;

import com.example.demo.model.Blog;
import com.example.demo.model.User;
import com.example.demo.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.HashMap;

@RestController
public class UpdateController {
    @Autowired
    private BlogService blogService;

    @RequestMapping("/updateLoad")
    public Object updateLoad(Integer blogId){
        HashMap<String, Object> map = new HashMap<>();
        if(blogId == null) {
            map.put("message","blogId丢失!");
            return map;
        }
        Blog blog = blogService.getBlogByBid(blogId);
        if(blog == null) {
            map.put("blog","不存在当前blog的文章!");
            return map;
        }
        return blog;
    }

    @RequestMapping("/update")
    public Object Update(Integer blogId, @RequestBody Blog blog, @SessionAttribute(value = "user",required = false)User user) {
        HashMap<String, Object> map = new HashMap<>();
        if(blogId == null) {
            map.put("message","blogId丢失!");
            return map;
        }
        blog.setBlogId(blogId);
        blog.setUserId(user.getUserId());
        blogService.updateBlog(blog);
        return map;
    }
}
